<!--
 * @Date: 2025-07-20 11:33:37
 * @LastEditTime: 2025-07-23 21:42:44
-->
<template>
  <div class="header-bar">
    <div class="sys-info">
      <div class="head-logo">
        <img src="@/assets/images/chat-logo-2.png" alt="logo" />
      </div>
      <div class="head-title">后台管理系统</div>
    </div>

    <el-dropdown placement="bottom" @command="handleCommand">
      <div class="user-info">
        <el-avatar :size="30" :src="avatar" />
        <div class="user-name">{{ account.username }}</div>
        <el-icon><ArrowDownBold /></el-icon>
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="logout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup lang="ts">
import avatar from "@/assets/images/avatar-dft.png";
import { ElMessageBox } from "element-plus";
import { useAccountStore } from "../../../stores/account";
import { storeToRefs } from "pinia";
import { useRouter } from "vue-router";

const router = useRouter();

const accountStore = useAccountStore();

const { account } = storeToRefs(accountStore);

const handleCommand = (command: string) => {
  console.log(command);
  switch (command) {
    case "logout": // 退出登录
      ElMessageBox.confirm("确定要退出登录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          accountStore.logout();
          router.replace("/login");
        })
        .catch(() => {});
      break;
  }
};
</script>

<style scoped lang="scss">
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;

  .sys-info {
    display: flex;
    align-items: center;

    .head-logo {
      height: 40px;
      margin-right: 10px;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .head-title {
      font-size: 18px;
      font-weight: bold;
    }
  }
  .user-info {
    display: flex;
    align-items: center;

    .user-name {
      margin-left: 10px;
      margin-right: 10px;
    }
  }
}
</style>
